/**
 * Created by yaoyujie on 2016/9/19.
 */
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {

        if (this.value && (this.value + '').length > 0) {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        }

    });
    return o;
};

function formatMoney(money){
    var tpMoney = '0.00';
    if(undefined != money){
        tpMoney = money;
    }
    tpMoney = new Number(tpMoney);
    if(isNaN(tpMoney)){
        return '0.00';
    }
    tpMoney = tpMoney.toFixed(2) + '';
    var re = /^(-?\d+)(\d{3})(\.?\d*)/;
    while(re.test(tpMoney)){
        tpMoney = tpMoney.replace(re, "$1,$2$3")
    }

    return tpMoney;
}


function formatDate(value){
    if(value==null)return '';
    if(value&&value.length>10)
        return value.replace(' ','<br/>');
    return value;
}
/**
 * 删除数组元素
 * @param index
 * @returns {boolean}
 */
Array.prototype.remove = function (index) {
    if (isNaN(index) || index >= this.length) {
        return false;
    }
    this.splice(index, 1);
}

var ConfirmModal = function (message, callback) {
    BootstrapDialog.confirm({
        title: '系统提示',
        message: '<div class="text-center">' + message + '</div>',
        size: BootstrapDialog.SIZE_SMALL,
        type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
        closable: true, // <-- Default value is false
        draggable: true, // <-- Default value is false
        btnCancelLabel: '取消', // <-- Default value is 'Cancel',
        btnCancelClass: 'btn-default btn-sm',
        btnOKLabel: '确定', // <-- Default value is 'OK',
        btnOKClass: 'btn-warning btn-sm', // <-- If you didn't specify it, dialog type will be used,
        callback: function (result) {
            // result will be true if button was click, while it will be false if users close the dialog directly.
            if (result) {
                callback();
            } else {
                console.log('取消了');
            }
        }
    });
};

var AlertModal = function (message, option) {
    var settings = {
        title: '系统提示',
        message: '<div class="text-center">' + message + '</div>',
        size: BootstrapDialog.SIZE_SMALL,
        type: BootstrapDialog.TYPE_WARNING,
        closable: true, // <-- Default value is false
        draggable: true, // <-- Default value is false
        buttonLabel: '确认',
        buttonClass: 'btn-warning btn-sm'
    };
    settings = $.extend(settings, option);
    BootstrapDialog.alert(settings);
}

/**
 * 打开一个Modal
 * @param option
 * option.tplId 模板ID
 * option.data 模板需要的数据
 * option.showOkBtn 显示保存button 默认true 为true的时候需要一个callback
 */
var DialogModal = function (option) {
    var html = template(option.tplId, option.data);
    if (false !== option.showOkBtn) {
        option.showOkBtn = true;
    }
    var settings = {
        closable: false,
        title: option.title,
        message: $(html),
        buttons: [
            {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            },

        ]
    };

    if (option.showOkBtn === true) {
        settings.buttons.push({
            label: '保存',
            cssClass: 'btn-primary',
            action: function (dialog) {
                option.callback(dialog);
            }
        });
    }
    settings = $.extend(true, settings, option);
    BootstrapDialog.show(settings);
};

function uploadFile(elmId, path) {
    var formData = new FormData();
    var files=$('#' + elmId).prop('files');
    //console.log(files[0]);
    formData.append('file', files[0]);
    formData.append("path",path);
    return $.ajax({
        url: $base+'/upload',
        async: false,
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
    }).done().responseJSON;
}
 
/**
 * 获取服务类别的数据
 * @param option
 * option.callback 返回数据后的回调函数
 */

/**
 * 获取服务类别的数据
 */
var serviceCtegoryData = function () {
    return $.ajax({
        type: 'GET',
        async: false,
        url: $base + '/goodsCategory/getCategoryList',
    }).done().responseJSON;

};

var serviceGoodsData = function () {
    return $.ajax({
        type:'GET',
        async:false,
        url: $base+'/serviceGoods/getGoodsList',
    }).done().responseJSON;
};
var isImgFile=function(fileId) {
    //判断图片类型
    var file = $('#'+fileId)[0].value;
    if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file)) {
        //alert("图片类型必须是.gif,jpeg,jpg,png中的一种")
        return false;
    }
    return true;
}


 
